package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsTypeDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.GoodsType;
import com.atguigu.jxc.service.GoodsTypeService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsTypeServiceimpl implements GoodsTypeService {

    @Autowired
    private GoodsTypeDao goodsTypeDao;

    @Override
    public String loadGoodsType() {

        //List<LoadGoodsType> goodsType = goodsTypeDao.getAll();
        //goodsType.stream().forEach(System.out::println);
        List<GoodsType> list = goodsTypeDao.getAll2();
        Map<String,Object> map = new HashMap<>();
        for (GoodsType goodsType : list) {
            List<Map<String,Object>> subList = new ArrayList<>();
            if (goodsType.getPId()==-1){
                putMap(map,goodsType,1);
                for (GoodsType type : list) {
                    if (type.getPId()==goodsType.getGoodsTypeId()){
                        Map<String,Object> subMap = new HashMap<>();
                        List<Map<String,Object>> sList = new ArrayList<>();
                        putMap(subMap, type,0);
                        for (GoodsType tp : list) {
                            if (tp.getPId()==type.getGoodsTypeId()){
                                Map<String,Object> ssbMap = new HashMap<>();
                                putMap(ssbMap,tp,0);
                                sList.add(ssbMap);
                                subMap.put("children",sList);
                            }
                        }
                        subList.add(subMap);
                        map.put("children",subList);
                    }
                }
            }
        }
        List<Map<String,Object>> list1 = new ArrayList<>();
        list1.add(map);
        System.out.println("map = " + map);
//        Gson gson = new Gson();
//        return gson.toJson(list1);
        return toGson(list1);
    }

    @Override
    public String save(String goodsTypeName, Integer pId) {
        Integer insert = null;
        if (pId==1||pId==-1){
            insert = goodsTypeDao.insert(goodsTypeName,pId,1);
        }else {
            insert = goodsTypeDao.insert(goodsTypeName,pId,0);
        }
        ServiceVO serviceVO = toService(insert);
        return toGson(serviceVO);
    }

    @Override
    public String delete(Integer goodsTypeId) {
        Integer delete = goodsTypeDao.delete(goodsTypeId);
        ServiceVO serviceVO = toService(delete);
        return toGson(serviceVO);
    }


    public Map<String,Object> putMap(Map<String,Object> map,GoodsType goodsType,Integer state){
        map.put("id",goodsType.getGoodsTypeId());
        map.put("text",goodsType.getGoodsTypeName());
        map.put("iconCls","goods-type");
        Map<String,Object> stateMap = new HashMap<>();
        stateMap.put("state",state);
        map.put("attributes",stateMap);
        if (state==1){
            map.put("state","closed");
        }else {
            map.put("state","open");
        }
        return map;
    }
    public String toGson(Object object){
        return new Gson().toJson(object);
    }
    public ServiceVO toService(Integer integer){
        ServiceVO serviceVO = null;
        if (integer!=null&&integer==1){
            serviceVO = new ServiceVO(100,"请求成功");
        }else {
            serviceVO = new ServiceVO(100,"请求失败");
        }
        return serviceVO;
    }
}
